package cn.chinaunicom.sdsi.activiti.example.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.image.ProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class ActivtiDemoService {

	/**
	 * 服务的接口
	 */
	@Autowired
    private ProcessEngine processEngine;
	
	/**
	 * 流程定义管理
	 */
	@Autowired
	private RepositoryService repositoryService;
	
	/**
	 * 流程实例
	 */
	@Autowired
	private RuntimeService runtimeService;
	
	/**
	 * 任务
	 */
	@Autowired
	private TaskService taskService;
	
	/**
	 * 历史数据
	 */
	@Autowired
	private HistoryService historyService;
	
	/**
	 * 人员身份管理
	 */
	@Autowired
	private IdentityService identityService;
	
	
	
	public void startProcess(String processDefinitionKey, String businessKey, Map<String, Object> variables) {
		/**
		 * 根据流程key去发起一个流程
		 */
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, variables);
		System.out.println(processInstance.getId());
	}
	
	public Object getImage(String processDefinitionId) {
		
		// 获取流程引擎的配置
		ProcessEngineConfigurationImpl processEngineConfiguration = (ProcessEngineConfigurationImpl) processEngine.getProcessEngineConfiguration();
		ProcessDiagramGenerator diagramGenerator = processEngineConfiguration.getProcessDiagramGenerator();
		

		BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);

		List<String> al = new ArrayList<>();
		
		al.add("");
		// 调用绘制器绘图方法绘制当前等待任务高亮的流程图
		return diagramGenerator.generateDiagram(bpmnModel, "png", al,
						Collections.<String>emptyList(), processEngineConfiguration.getActivityFontName(),
						processEngineConfiguration.getLabelFontName(), null, null, 1.0);

	}
	
	public Deployment deployProcess(MultipartFile file,String key,String name, String tenantId) {
		InputStream fileInputStream = null;
		try {
			fileInputStream = file.getInputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return repositoryService.createDeployment().key(key).name(name).tenantId(tenantId).addInputStream(file.getOriginalFilename(),fileInputStream).deploy();
	}
	
	
	public void taskComplate(String taskId, String userId) {
		
		Map<String, Object> variables = new HashMap<>();
		variables.put("assigenn", userId);
		taskService.complete(taskId, variables);
//		taskService.claim(taskId, userId); //认领
//		taskService.resolveTask(taskId, variables); //释放
		
		
	}
	
	
}
